package com.cf.game.im.handler.ack;

import com.cf.game.im.dao.ChatMapper;
import com.cf.game.im.handler.Handler;
import com.cf.game.msg.ChatMessageReadRequest;
import com.cf.mapper.BeanMapper;
import com.cf.redis.core.RedisPusher;
import com.cf.util.ObjectJsonUtil;
import io.vertx.core.eventbus.EventBus;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * boot-game-HasReadHandler
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/8/1 下午 12:32
 */
@Slf4j
@Component
public class ReadOutHandler implements Handler {

    @Autowired
    private BeanMapper beanMapper;

    @Autowired
    private ChatMapper chatMapper;

    @Autowired
    private RedisPusher redisPusher;

    @Override
    public void handle(Map dataMap, EventBus eventBus) {
        //获取聊天历史记录
        ChatMessageReadRequest chatMessageReadRequest = beanMapper.map(dataMap, ChatMessageReadRequest.class);

        if (StringUtils.isEmpty(chatMessageReadRequest.getUinId())) {
            log.warn("此消息无UinId【{}】", ObjectJsonUtil.toString(chatMessageReadRequest));
            return;
        }
        chatMapper.updateChatMessageRead(chatMessageReadRequest.getUinId());
        Map extra = chatMessageReadRequest.getExtra();
        if (extra == null || extra.get("follow") == null) {
            log.info("不存在需要通知的对象，read消息处理结束！");
            return;
        }

        String follow = String.valueOf(extra.get("follow"));
        redisPusher.convertAndSend(follow, ObjectJsonUtil.toString(chatMessageReadRequest));

    }

    @Override
    public String getSupport() {
        return "readOut";
    }
}
